package net.mikoo.seals.utils;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class ObjectUtil {
	
	public static boolean isNone(String st) {
		return st == null || st.trim().equals("");
	}
	
	public static String getExtention(String fileName) {
		int pos = fileName.lastIndexOf(".");
		return fileName.substring(pos);
	}
	
	public static String getString(Object st) {
		return st == null ? "" : (st.toString()).trim();
	}
	
	public static int getInt(String st) {
		return st == null || (st.trim()).equals("") ? 0 : Integer.parseInt(st.trim());
	}
	
	public static int getInt(Object st) {
		return st == null || (st.toString().trim()).equals("") ? 0 : Integer.parseInt(st.toString()
			.trim());
	}
	
	public static int lastlogintime(Object st) {
		return st == null || (st.toString().trim()).equals("") ? 0 : Integer.parseInt(st.toString()
			.trim());
	}
	
	public static long getLong(String st) {
		return st == null || (st.trim()).equals("") ? 0 : Long.parseLong(st.trim());
	}
	
	public static long getLong(Object st) {
		return st == null || (st.toString()).equals("") ? 0l : Long.parseLong(st.toString());
	}
	
	public static double getDouble(String st) {
		return st == null || (st.trim()).equals("") ? 0 : Double.parseDouble(st.trim());
	}
	
	public static double getDouble(Object st) {
		return st == null ? 0 : Double.parseDouble(st.toString());
	}
	
	public static float getFloat(String st) {
		return st == null || (st.trim()).equals("") ? 0 : Float.parseFloat(st.trim());
	}
	
	public static float getFloat(Object st) {
		return st == null || (st.toString().trim()).equals("") ? 0 : Float.parseFloat(st.toString()
			.trim());
	}
	
	public static boolean getBoolean(Object o) {
		return null == o ? false : Boolean.getBoolean(o.toString());
	}
	
	public static int testUrl(String url) {
		try {
			
			System.out.println("test url : " + url);
			
			long start = System.currentTimeMillis();
			
			HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
			int code = connection.getResponseCode();
			connection.disconnect();
			System.out.println(url + " 用时" + (System.currentTimeMillis() - start));
			
			return code;
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return 400;
	}
}
